const AccountKey = 'augurit-account';
const PasswordKey = 'augurit-password';
const TokenKey = 'augurit-token';
const UserKey = 'augurit-user';
const PermKey = 'augurit-perms';
const AreaCodeKey = 'augurit-areacode';
const AreaCodeType = 'augurit-areatype';

let local = {
    setCache(key, cache) {
        localStorage.setItem(`augurit-cache-${key}`, cache);
    },
    getCache(key) {
        if (localStorage.getItem(`augurit-cache-${key}`) === '') {
            return null;
        }
        return localStorage.getItem(`augurit-cache-${key}`);
    },
    // 登录账号缓存
    setAccount: (account) => {
        localStorage.setItem(AccountKey, account);
    },
    getAccount: () => {
        return localStorage.getItem(AccountKey);
    },
    removeAccount: () => {
        localStorage.removeItem(AccountKey);
    },
    // 登录密码缓存
    setPassword: (password) => {
        localStorage.setItem(PasswordKey, password);
    },
    getPassword: () => {
        return localStorage.getItem(PasswordKey);
    },
    removePassword: () => {
        localStorage.removeItem(PasswordKey);
    },
    // 用户Token缓存
    setToken: (token) => {
        sessionStorage.setItem(TokenKey, token);
    },
    getToken: () => {
        return sessionStorage.getItem(TokenKey);
    },
    removeToken: () => {
        sessionStorage.removeItem(TokenKey);
    },
    // 用户信息缓存
    setUser: (user) => {
        sessionStorage.setItem(UserKey, JSON.stringify(user))
    },
    getUser: () => {
        let userStr = sessionStorage.getItem(UserKey);
        return userStr ? JSON.parse(userStr) : null
    },
    removeUser: () => {
        sessionStorage.removeItem(UserKey);
    },
    // 用户权限缓存
    setPerm: (perms) => {
        sessionStorage.setItem(PermKey, JSON.stringify(perms))
    },
    getPerm: () => {
        let permStr = sessionStorage.getItem(PermKey);
        return permStr ? JSON.parse(permStr) : null
    },
    removePerm: () => {
        sessionStorage.removeItem(PermKey);
    },
    hasPerm: (code) => {
        let permStr = sessionStorage.getItem(PermKey);
        permStr = permStr ? JSON.parse(permStr) : {};
        console.log(permStr, code, permStr[code]);
        return !!permStr[code];
    },
    //当前选择的区域信息
    setAreaCode: (areaCode) => {
        localStorage.setItem(AreaCodeKey, areaCode);
    },
    getAreaCode: () => {
        return localStorage.getItem(AreaCodeKey);
    },
    setAreaType: (areaType) => {
        localStorage.setItem(AreaCodeType, areaType);
    },
    getAreaType: () => {
        return localStorage.getItem(AreaCodeType);
    },
}

export default local